<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<title>审批</title>
	<link rel="stylesheet" href="assets/libs/layui/css/layui.css" />
	<style>
		@media screen and (min-width: 800px) {
			.layui-form-label{
				width: 86px !important;
			}
			.common_info{
				width: 300px !important;
			}
		}

		.content_box{
			max-width: 960px;
			display: flex;
			justify-content: center
		}

		.login-header {
			color: rgba(0, 0, 0, .85) !important;
			padding: 5% 0;
			text-align: center;
		}

		.buttongroup {
			display: flex;
			margin-top: 10%;
		}

		.buttongroup button {
			width: 100%;
			height: 45px;
		}

		.main_content_wrap {
			margin: 20px 0px;
		}
	</style>
</head>

<body>
	<div class="body">
		<div class="login-header">
			<h2 style="font-weight: 400;letter-spacing: 3px; font-size: 30px" id="show_title">二级单位审批</h2>
		</div>
		<div class="layui-fluid content_box">
			<div class="main_content_wrap">
				<form lay-filter="modifyForm" class="layui-form model-form">
					<div class="layui-form-item">
						<label class="layui-form-label">审批状态</label>
						<div class="layui-input-inline">
							<input type="radio" name="opt_state" lay-filter="opt_state" value="1" title="同意" checked/>
							<input type="radio" name="opt_state" lay-filter="opt_state" value="2" title="不同意"/>
						</div>
					</div>
					<div class="layui-form-item" id="show_department" style="display: none">
						<label class="layui-form-label">部门负责人</label>
						<div class="layui-input-inline">
							<div class="layui-input" id="selUser">
								<ww-open-data class="opendata-show" type="userName" openid="" id="departmentUserData"></ww-open-data>
							</div>
						</div>
						<input name="check_department_leader_id" id="check_department_leader_id" hidden />
					</div>
					<div class="layui-form-item" id="show_vice_principal" style="display: none">
						<label class="layui-form-label">副校长</label>
						<div class="layui-input-inline">
							<div class="layui-input" id="selPrincipal">
								<ww-open-data class="opendata-show" type="userName" openid="" id="principalUserData"></ww-open-data>
							</div>
						</div>
						<input name="check_vice_principal_id" id="check_vice_principal_id" hidden />
					</div>
					<div class="layui-form-item" id="show_common">
						<label class="layui-form-label">备注</label>
						<div class="layui-input-inline common_info">
							<textarea name="opt_common" id="opt_common" placeholder="请输入备注" class="layui-textarea"></textarea>
						</div>
					</div>
					<div class="layui-form-item text-center buttongroup">
						<button type="button" lay-filter="modifyFormSubmit" lay-submit
							class="layui-btn site-demo-active">审批</button>
					</div>
				</form>
			</div>
		</div>
	</div>
	<script type="text/javascript" src="assets/libs/layui/layui.js"></script>
	<script type="text/javascript" src="assets/js/config.js"></script>
	<script type="text/javascript" src="assets/js/wechatApi.js"></script>
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

	<script src="https://wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-1.3.1.js"></script>
	<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js" referrerpolicy="origin"></script>
	<script>
		layui.config({
			base: 'assets/module/'
		}).use(['jquery', 'form', 'layer'], function () {
			var $ = layui.jquery;
			var form = layui.form;
			var layer = layui.layer;

			let userMsg = JSON.parse(localStorage.getItem("userMsg"));
			toRegister(userMsg);

			var content = {};
			var id = getUrlParam('id');
			var opt_type = getUrlParam('type')?getUrlParam('type'):2; //获取id
			var type_detail = getUrlParam('type_detail')?getUrlParam('type_detail'):0; //获取id
			var need_department_leader = false;
			var need_vice_principal = false;
			form.render();
			$.ajax({
				type: 'get',
				url: serverUrl + 'Knowledge/getDetail',
				data: {
					'id': id
				},
				dataType: "json",
				success: function (res) {
					if (res.success === true) {
						content = res.obj;
						if(opt_type != 2){
							type_detail=1;
							if(content.check_department_leader_id && !content.check_department_leader){
								type_detail = 2;
							}else if(content.check_vice_principal_id && !content.check_vice_principal){
								type_detail = 3;
							}
							getMoneyCheckList();
						}
					}
				},
				error: function (error) {
					console.log(error);
				}
			})
			var show_title = opt_type == 2 ? '二级单位审批':'金额审批'
			$('#show_title').html(show_title);
			form.on('radio(opt_state)', function(data){
				$("#opt_common").val('');
				$("#department_leader_id").val('')
				$("#vice_principal_id").val('')
				if(data.value == 1){
					if(need_department_leader){
						$("#show_department").show();
					}
					if(need_vice_principal){
						$("#show_vice_principal").show();
					}
				}else{
					$("#show_department").hide();
					$("#show_vice_principal").hide();
				}
				form.render();
			});

			$('#selUser').on('click', function () {
				document.getElementById('departmentUserData').setAttribute('openid', '');
				WWOpenData.bind(document.querySelector('ww-open-data'));
				selectEnterpriseContact('user','single',[],[],
						function (res) {
							const openid = res.ids[0]
							$('#check_department_leader_id').val(openid)
							document.getElementById('departmentUserData').setAttribute('openid', openid);
							WWOpenData.bind(document.querySelector('ww-open-data'));
							form.render();
						},
						function (err) {
							console.log('选择失败：', err)
						}
				)
			});

			$('#selPrincipal').on('click', function () {
				document.getElementById('principalUserData').setAttribute('openid', '');
				WWOpenData.bind(document.querySelector('ww-open-data'));
				selectEnterpriseContact('user','single',[],[],
						function (res) {
							const openid = res.ids[0]
							$('#check_vice_principal_id').val(openid)
							document.getElementById('principalUserData').setAttribute('openid', openid);
							WWOpenData.bind(document.querySelector('ww-open-data'));
							form.render();
						},
						function (err) {
							console.log('选择失败：', err)
						}
				)
			});

			//监听提交
			form.on('submit(modifyFormSubmit)', function (data) {
				var DISABLED = 'layui-btn-disabled';
				// 增加样式
				$('.site-demo-active').addClass(DISABLED);
				// 增加属性
				$('.site-demo-active').attr('disabled', 'disabled');

				var form_data = data.field;
				form_data.opt_json_content = JSON.stringify(form_data);
				form_data.knowledge_id = id
				form_data.opt_type = opt_type
				if(type_detail){
					form_data.opt_type_detail = type_detail
				}
				form_data.opt_userid = userMsg.UserId;
				form_data.opt_deptment = Array.isArray(userMsg.department)?userMsg.department.join(','):userMsg.department;
				let newobj = typeChange(form_data);
				$.post(serverUrl + 'Knowledge/modifyInfo.html', newobj, function (res) {
					if (res.success === true) {
						layer.msg('提交成功', { icon: 1, time: 300 }, function () {
							window.location.href = `detail.html?id=`+id;
						});
					} else {
						layer.msg(res.msg, { icon: 2 });
						$('.site-demo-active').removeClass(DISABLED);
						$('.site-demo-active').removeAttr('disabled');
					}
				}, 'json');
				return false;
			});

			function getMoneyCheckList() {
				var level = 0;
				var show_info = [];
				$.get(serverUrl + 'Common/getList.html', {type:'money_check'}, function (res) {
					if (res.success) {
						for (var i = 0; i < res.obj.length; i++) {
							if(res.obj[i].name){
								if(content.conversion_get_money < parseFloat(res.obj[i].name) && level==0){
									level = 1;
									show_info = res.obj[i].show_info.split(',');
								}
							}else{
								if(level==0){
									show_info = res.obj[i].show_info.split(',');
								}
							}
						}
						if(show_info.indexOf('department_leader')>=0 && type_detail == 1){
							need_department_leader = true;
							$("#show_department").show();
						}
						if(show_info.indexOf('vice_principal')>=0 && type_detail == 2){
							need_vice_principal = true;
							$("#show_vice_principal").show();
						}
					}
				}, 'json');
			}
		});
	</script>
</body>

</html>